# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from basecart import BaseCart

class cart_insurance(BaseCart):
    def test_cart_insurance(self):
        driver = self.driver
        current_price = 10000
        while current_price>1500 or current_price<800:
            self.get_url("/category/aksessyary_dlya_mobilnikh.html")
            itm_url = self.get_item()
            current_price = self.current_price()
        self.buy_item()
        #If ok go to cart
        self.go_cart()
        cred_block = self.perform_order()
        #Base price
        base_price = int(WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                                 .find_element_by_css_selector("b.js-total-value")).text.replace(" ", ""))
        #Go fuc.. post.
        post_rb = WebDriverWait(driver, timeout=10)\
                   .until(lambda dr: [a for a in dr.find_elements_by_css_selector("span.g-pseudo-h.g-link") if a.text==u'Почта'])[0]
        post_rb.click()
        #Post tooltip
        post_tlt = WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                                                    .find_element_by_css_selector("div.b-tooltip-body"))
        ActionChains(driver).move_to_element(post_tlt).perform()
        #Get variant
        variant = WebDriverWait(post_tlt, timeout=4).until(lambda dr: [a for a in dr\
                           .find_elements_by_css_selector("label.b-form-list-item")])[0]
        variant.click()
        #Insurance price
        insprtext = WebDriverWait(post_tlt, timeout=2).until(lambda dr: [a.text for a in dr\
                          .find_elements_by_css_selector("span.g-font.color_lgray") if '+' in a.text])[0]
        insprice = int(re.sub('\D', '', insprtext))
        #Delivery price
        deltext = '0(zero)'
        try:
            deltext = WebDriverWait(post_tlt, timeout=2).until(lambda dr: [a.text for a in dr\
                              .find_elements_by_css_selector("p.g-font.color_lgray") if u'дней' in a.text])[0]\
                              .split(',')[0]
        except:
            print "Possible delivery is for free"
        delprice = int('0'+re.sub('\D', '', deltext))
        #Get insurance
        insrnc = WebDriverWait(driver, timeout=10).until(lambda dr: [a for a in dr\
                          .find_elements_by_name("ot_insurance") if a.get_attribute("value")=='yes'])[0]
        insrnc.click()
        #Wait for price changed
        WebDriverWait(driver, timeout=10).until(lambda dr: not self.price_in_order() == base_price)
        #check price
        new_price = self.price_in_order()
        print "insprtext=="+insprtext
        print "deltext=="+deltext
        self.assertEqual(base_price, new_price - insprice - delprice)
